package com.example.lemis.web.controller;

import com.example.lemis.common.exception.NotLoginException;
import com.example.lemis.model.dto.AdminLoginRequest;
import com.example.lemis.model.dto.PageDTO;
import com.example.lemis.model.vo.AdminInfoVO;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.lemis.common.JsonResponse;
import com.example.lemis.service.IAdminService;
import com.example.lemis.model.domain.Admin;
import org.springframework.web.server.ResponseStatusException;


/**
 *
 *  前端控制器
 *
 *
 * @author lxp
 * @since 2025-06-24
 * @version v1.0
 */
@RestController
@RequestMapping("/api/admin")
public class AdminController {

    private final Logger logger = LoggerFactory.getLogger( AdminController.class );

    @Autowired
    private IAdminService adminService;


    /**
    * 描述：根据Id 查询
    *
    */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse<Admin> getById(@PathVariable("id") Long id)throws Exception {
        Admin admin = adminService.getById(id);
        return JsonResponse.success(admin);
    }

    @PostMapping("/login")
    public JsonResponse<AdminInfoVO> login(@Validated @RequestBody AdminLoginRequest adminIdentity) throws Exception {
        AdminInfoVO admin = adminService.login(adminIdentity);
        if (admin == null) {
            throw new NotLoginException("工号或密码错误");
        }
        return JsonResponse.success(admin, "登录成功");
    }

    @GetMapping("/auth")
    public JsonResponse<AdminInfoVO> auth() {
        AdminInfoVO adminInfo = adminService.auth();
        if (adminInfo == null) {
            throw new ResponseStatusException(
                    HttpStatus.UNAUTHORIZED, "您还未登录");
        }
        return JsonResponse.success(adminInfo, "已登录");
    }

    @GetMapping("/list")
    public JsonResponse<?> listAdmins(PageDTO pageDTO) {
        // 这里可以添加业务逻辑，比如查询所有管理员
        // 假设我们直接返回一个空的列表作为示例
        return JsonResponse.success(adminService.getAdminList(pageDTO), "管理员列表");
    }
}

